package com.enation.app.web.core.plugin.member;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.enation.app.base.core.model.PluginDataMapper;
import com.enation.app.web.core.model.Address;
import com.enation.app.web.core.model.Member;
import com.enation.app.web.core.service.IAddressManager;
import com.enation.app.web.core.service.IMemberManager;
import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;
import com.enation.framework.plugin.AutoRegisterPluginsBundle;
import com.enation.framework.plugin.IPlugin;
import com.enation.framework.util.JsonUtil;

/**
 * 
 * @function 会员插件桩
 * @author 小韦
 * @time 2017年1月14日 下午10:24:27
 */
@Service("memberPluginBundle")
public class MemberPluginBundle extends AutoRegisterPluginsBundle {

	@Autowired
	private IMemberManager memberManager;

	@Autowired
	private IAddressManager addressManager;

	@Override
	public String getName() {

		return "会员插件桩";
	}

	public List<PluginDataMapper> getDetailHtml(Integer member_id) {
		List<PluginDataMapper> list = new ArrayList<PluginDataMapper>();

		FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
		List<IPlugin> plugins = this.getPlugins();

		Member member = this.memberManager.findById(member_id);
		freeMarkerPaser.putData("member", member);
		List<Address> provinces = this.addressManager.getProvince();
		freeMarkerPaser.putData("provinces", provinces);
		if (member.getProvince_id() != null) {
			if (member.getProvince_id() != 0) {

				List<Address> citys = this.addressManager.getParentByPid(member
						.getProvince_id());
				freeMarkerPaser.putData("citys", citys);

			}
		}
		if (member.getCity_id() != null) {
			List<Address> regions = this.addressManager.getParentByPid(member
					.getCity_id());
			Address address = regions.get(0);
			regions.remove(0);
			List<Address> secondRegions = this.addressManager
					.getParentByPid(address.getId());
			for (Address address2 : regions) {
				secondRegions.add(address2);
			}
			freeMarkerPaser.putData("regions", secondRegions);
		}

		// 获取登录情况
		String loginString = member.getLogincount();
		Map<String, Object> loginMap = JsonUtil.toMap(loginString);
		freeMarkerPaser.putData("loginMap", loginMap);

		if (plugins != null) {

			for (IPlugin plugin : plugins) {

				if (plugin instanceof IMemberTabShowEvent) {

					PluginDataMapper dataMapper = new PluginDataMapper();
					IMemberTabShowEvent event = (IMemberTabShowEvent) plugin;

					freeMarkerPaser.setClz(event.getClass());

					freeMarkerPaser.setPageName(event.onShow());

					dataMapper.setOrder(event.getOrder());
					dataMapper.setTabTitle(event.getTabName());
					dataMapper.setTabHtml(freeMarkerPaser.proessPageContent());

					list.add(dataMapper);
				}
			}
		}

		return list;
	}

}
